﻿using System;
using System.ComponentModel.Composition;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Parts;

namespace ASPItalia.ModelVirtualCasting.EntityFramework
{
	[Export("CandidateRepository", typeof(ICandidateRepository))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	[RepositoryMetadata(Name = "CandidateRepository", IsFake = false)]
	public class CandidateRepository : BaseRepository<Candidate>, ICandidateRepository
	{
		[ImportingConstructor()]
		public CandidateRepository(IContextContainer container)
			: base(container)
		{
		}

		public void InsertOnSubmit(Candidate entity)
		{
			if (entity.ResidenceCountry != null)
			{
				entity.ResidenceCountryID = entity.ResidenceCountry.CountryID;
				entity.ResidenceCountry = null;
			}
			if (entity.NationalityCountry != null)
			{
				entity.NationalityCountryID = entity.NationalityCountry.CountryID;
				entity.NationalityCountry = null;
			}
			if (entity.JobLevel != null)
			{
				entity.JobLevelID = entity.JobLevel.JobLevelID;
				entity.JobLevel = null;
			}
			else
			{
				entity.JobLevelID = 1;
			}
			if (entity.PrimaryLanguage != null)
			{
				entity.PrimaryLanguageID = entity.PrimaryLanguage.LanguageID;
				entity.PrimaryLanguage = null;
			}
			else
			{
				entity.PrimaryLanguageID = 1;
			}
			if (entity.SecondaryLanguage != null)
			{
				entity.SecondaryLanguageID = entity.SecondaryLanguage.LanguageID;
				entity.SecondaryLanguage = null;
			}
			entity.InsertionDate = DateTime.Now;
			CurrentSet.Add(entity);

			entity.Availability = "N";
		}

		public void Update(Candidate entity)
		{
			CurrentSet.Attach(entity);
			Context.ChangeObjectState(entity, EntityState.Modified);
		}

		public void DeleteOnSubmit(Candidate entity)
		{
			CurrentSet.Remove(entity);
		}

		public void SubmitChanges()
		{
			SaveChanges();
		}

		public IQueryable<Candidate> GetAll()
		{
			return CurrentQueryableSet.OfType<Candidate>();
		}

		public IQueryable<Candidate> Where(Expression<Func<Candidate, bool>> predicate)
		{
			return CurrentQueryableSet.OfType<Candidate>().Where(predicate);
		}

		public long Count(Expression<Func<Candidate, bool>> predicate)
		{
			return CurrentQueryableSet.OfType<Candidate>().Count(predicate);
		}

		public Candidate GetByID(int id)
		{
			return CurrentQueryableSet.OfType<Candidate>().First(p => p.ID == id);
		}

		public ICandidateRepository Include<TProperty>(Expression<Func<Candidate, TProperty>> path)
		{
			IncludeInternal(path);
			return this;
		}

		public ICandidateRepository Include(string path)
		{
			IncludeInternal(path);
			return this;
		}

		public ICandidateRepository CreateQuery()
		{
			CreateQueryInternal();
			return this;
		}

		public ICandidateRepository EnableLazyLoading()
		{
			SetLazyLoading(true);
			return this;
		}

		public ICandidateRepository DisableLazyLoading()
		{
			SetLazyLoading(false);
			return this;
		}

		public ICandidateRepository DoNotTrack()
		{
			base.DoNotTrackInternal();
			return this;
		}
	}
}